/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Presentacion;

import ModeloLogicoSistema.AdministradorServicio;
import ModeloLogicoSistema.Servicio.TipoServicio;
import ModeloLogicoSistema.Validacion;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;

/**
 *
 * @author debo
 */


public class AltaServicio extends javax.swing.JDialog {

    
    

/**
 *
 * @author Veko
 */
    ButtonGroup butonGroup = null;
    AdministradorServicio administradorServicio = null;
 
    /**
     * Creates new form AltaServicio
     */
    public AltaServicio(java.awt.Frame parent, boolean modal) throws SQLException {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(null);
        for(TipoServicio tipo: TipoServicio.values()){
            this.cbTipoServicio.addItem(tipo);
        }
        
        try {
            butonGroup = new ButtonGroup();
            administradorServicio = new AdministradorServicio();
        } catch (SQLException ex) {
            Logger.getLogger(AltaServicio.class.getName()).log(Level.SEVERE, null, ex);
        }
        Integer idServicio = administradorServicio.nuevoCodigoServicio();
        txtIdServicio.setText(idServicio.toString());
        labelPrecioT.setVisible(false);
        precioT.setVisible(false);
        butonGroup.add(RBlocal);
        butonGroup.add(JBter);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        labelPrecio = new javax.swing.JLabel();
        txtIdServicio = new javax.swing.JTextField();
        txtNombre = new javax.swing.JTextField();
        txtPrecio = new javax.swing.JTextField();
        RBlocal = new javax.swing.JRadioButton();
        JBter = new javax.swing.JRadioButton();
        precioT = new javax.swing.JTextField();
        BtnAceptar = new javax.swing.JButton();
        BtnLimpiar = new javax.swing.JButton();
        BtnCerrar = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        cbTipoServicio = new javax.swing.JComboBox();
        labelErrorNombre = new javax.swing.JLabel();
        labelErrorNombre.setVisible(false);
        labelPrecioError = new javax.swing.JLabel();
        labelLocalTercError = new javax.swing.JLabel();
        labelPrecioTercError = new javax.swing.JLabel();
        labelPrecioT = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Alta de Servicios");

        jLabel1.setText("ID Servicio");

        jLabel2.setText("Nombre");

        labelPrecio.setText("Precio");

        txtIdServicio.setEditable(false);
        txtIdServicio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtIdServicioActionPerformed(evt);
            }
        });

        txtNombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNombreActionPerformed(evt);
            }
        });

        txtPrecio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPrecioActionPerformed(evt);
            }
        });

        RBlocal.setText("Local");
        RBlocal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RBlocalActionPerformed(evt);
            }
        });

        JBter.setText("Terciarizado");
        JBter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JBterActionPerformed(evt);
            }
        });

        precioT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                precioTActionPerformed(evt);
            }
        });

        BtnAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Guardar.png"))); // NOI18N
        BtnAceptar.setText("Aceptar");
        BtnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnAceptarActionPerformed(evt);
            }
        });

        BtnLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/limpiar.png"))); // NOI18N
        BtnLimpiar.setText("Limpiar");
        BtnLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnLimpiarActionPerformed(evt);
            }
        });

        BtnCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/cancelar.png"))); // NOI18N
        BtnCerrar.setText("Cerrar");
        BtnCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnCerrarActionPerformed(evt);
            }
        });

        jLabel5.setText("Tipo de Servicio");

        labelErrorNombre.setForeground(new java.awt.Color(255, 0, 0));
        labelErrorNombre.setText("Campo Obligatorio");

        labelPrecioError.setForeground(new java.awt.Color(255, 0, 0));
        labelPrecioError.setText("Campo Obligatorio");
        labelPrecioError.setVisible(false);

        labelLocalTercError.setForeground(new java.awt.Color(255, 0, 0));
        labelLocalTercError.setText("Selección Obligatoria");
        labelLocalTercError.setVisible(false);

        labelPrecioTercError.setForeground(new java.awt.Color(255, 0, 0));
        labelPrecioTercError.setText("Campo Obligatorio ");
        labelPrecioTercError.setVisible(false);

        labelPrecioT.setText("Precio");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jSeparator1))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(RBlocal))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(labelLocalTercError, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(cbTipoServicio, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addComponent(jLabel1)
                                                    .addComponent(jLabel2)
                                                    .addComponent(labelPrecio))
                                                .addGap(44, 44, 44))
                                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                    .addComponent(labelPrecioError)
                                                    .addComponent(labelErrorNombre))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(txtIdServicio, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(txtPrecio, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(BtnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(BtnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(JBter)
                                        .addGap(66, 66, 66)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(labelPrecioTercError, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(labelPrecioT, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(precioT, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(95, 95, 95)
                                        .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(0, 55, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(BtnCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGap(23, 23, 23))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtIdServicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(labelErrorNombre)
                        .addGap(7, 7, 7)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtPrecio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labelPrecio))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(2, 2, 2))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(labelPrecioError)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(cbTipoServicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(RBlocal)
                    .addComponent(labelLocalTercError))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(JBter)
                    .addComponent(precioT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelPrecioT))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 6, Short.MAX_VALUE)
                .addComponent(labelPrecioTercError)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BtnAceptar)
                    .addComponent(BtnLimpiar)
                    .addComponent(BtnCerrar))
                .addGap(26, 26, 26))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void precioTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_precioTActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_precioTActionPerformed

    private void BtnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnAceptarActionPerformed
        // Aceptar
        
        String nombre= txtNombre.getText();
        if(Validacion.soloNumeros(nombre)){
            JOptionPane.showMessageDialog(null, "No puede ingresar un nombre compuesto solo por números. Seleccione otro nombre por favor.", "Nombre", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        Double pre=null;
        if(nombre.isEmpty() | (!RBlocal.isSelected() & !this.JBter.isSelected())){
            this.labelErrorNombre.setVisible(true);
            this.labelLocalTercError.setVisible(true);
            this.labelPrecioError.setVisible(true);
            return;
        }
        try{
            //le paso menos uno porque no existe ne la base de datos
            if(this.administradorServicio.nombreDeServicioTomado(nombre,-1)){
                JOptionPane.showMessageDialog(null, "El nombre elegido para el servicio ya existe. Seleccione otro por favor.", "Nombre existente", JOptionPane.INFORMATION_MESSAGE);
                return;
            }   
        }catch (Exception e){}
        if(Validacion.preciosValidos(txtPrecio.getText())){
            pre = new Double(txtPrecio.getText());
        }else{
            this.labelErrorNombre.setVisible(true);
            this.labelLocalTercError.setVisible(true);
            this.labelPrecioError.setVisible(true);
            return;
        }
        
        String tipo=this.cbTipoServicio.getSelectedItem().toString();
        
        Double preT;
        boolean esTercerizado = false;
        if (RBlocal.isSelected() == true) {
            preT = new Double("0.0");
        } else {
            if(Validacion.preciosValidos(precioT.getText())){
                 preT = new Double(precioT.getText());
                esTercerizado = true;
            }else{
                this.labelPrecioTercError.setVisible(true);
                return;
            }
       }

        AdministradorServicio admServicio = null;
        try {
            admServicio = new AdministradorServicio();
        } catch (SQLException ex) {
            Logger.getLogger(AltaServicio.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            admServicio.persistirServicio(nombre, pre, esTercerizado, preT, tipo);
        } catch (SQLException ex) {
            Logger.getLogger(AltaServicio.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.dispose();
    }//GEN-LAST:event_BtnAceptarActionPerformed

    private void BtnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnLimpiarActionPerformed
        txtNombre.setText(null);
        txtPrecio.setText(null);
        precioT.setText(null);
        this.JBter.setSelected(false);
        this.RBlocal.setSelected(false);
    }//GEN-LAST:event_BtnLimpiarActionPerformed

    private void BtnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnCerrarActionPerformed
        BuscarServicios buscarServicios = new BuscarServicios(null, rootPaneCheckingEnabled);
        buscarServicios.setVisible(false);
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_BtnCerrarActionPerformed

    private void JBterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JBterActionPerformed
        if(!labelPrecioT.isVisible())
            labelPrecioT.setVisible(true);
        precioT.setVisible(true);
    }//GEN-LAST:event_JBterActionPerformed

    private void RBlocalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RBlocalActionPerformed
        if(labelPrecioT.isVisible())
            labelPrecioT.setVisible(false);
        precioT.setVisible(false);
    }//GEN-LAST:event_RBlocalActionPerformed

    private void txtIdServicioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtIdServicioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtIdServicioActionPerformed

    private void txtNombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNombreActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNombreActionPerformed

    private void txtPrecioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPrecioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPrecioActionPerformed

    /**
     * @param args the command line arguments
     */
   // public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
/*        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AltaServicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AltaServicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AltaServicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AltaServicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
     /*   java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                AltaServicio dialog = null;
                try {
                    dialog = new AltaServicio(new javax.swing.JFrame(), true);
                } catch (SQLException ex) {
                    Logger.getLogger(AltaServicio.class.getName()).log(Level.SEVERE, null, ex);
                }
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnAceptar;
    private javax.swing.JButton BtnCerrar;
    private javax.swing.JButton BtnLimpiar;
    private javax.swing.JRadioButton JBter;
    private javax.swing.JRadioButton RBlocal;
    private javax.swing.JComboBox cbTipoServicio;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel labelErrorNombre;
    private javax.swing.JLabel labelLocalTercError;
    private javax.swing.JLabel labelPrecio;
    private javax.swing.JLabel labelPrecioError;
    private javax.swing.JLabel labelPrecioT;
    private javax.swing.JLabel labelPrecioTercError;
    private javax.swing.JTextField precioT;
    private javax.swing.JTextField txtIdServicio;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtPrecio;
    // End of variables declaration//GEN-END:variables
}
